package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DocumentUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SaveServiceImpl implements SaveService {

    @Autowired
    private IBdMaterialService bdMaterialService;
    @Autowired
    private IPoStorereqBService poStorereqBService;
    @Autowired
    private IPoStorereqService poStorereqService;
    @Autowired
    private IIcSapplyBService iIcSapplyBService;
    @Autowired
    private IIcSapplyHService iIcSapplyHService;
    @Autowired
    private SelectBoxService selectBoxService;

    @Override
    public AjaxResult savePurchase(RequestPurchase requestPurchase) {

        //获取集团信息
        String pkGrop = bdMaterialService.selectPkGrop();
//        String pkGrop = "0001A1100000000033YY";
        //拿到表单的表头数据
        PoStorereq poStorereq = requestPurchase.getPoStorereq();
        //设置表头数据的值
        //设置唯一主键
        String purchaseId = DocumentUtils.getPurchaseId();
        poStorereq.setPkStorereq(purchaseId);
        //部门信息
        poStorereq.setPkAppdepth(poStorereq.getPkAppdepthV());
        String deptV = selectBoxService.getDeptV(poStorereq.getPkAppdepthV());
        poStorereq.setPkAppdepthV(deptV);
        //设置库存保本号
        String orgStockorgV = selectBoxService.getOrgStockorgV(poStorereq.getPkOrg());
        poStorereq.setPkOrgV(orgStockorgV);
        //设置申请单号
        String applicationNumber = DocumentUtils.getApplicationNumber();
        poStorereq.setVbillcode(applicationNumber);//申请单号
        //设置单据的状态
        poStorereq.setFbillstatus(0);//单据状态
        poStorereq.setPkGroup(pkGrop);
        //设置制单人
        poStorereq.setBillmaker(poStorereq.getPkApppsnh());
        //是否紧急
        poStorereq.setBurgency("N");
        //创建时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        poStorereq.setDbilldate(format);
        poStorereq.setCreationtime(format);
        //创建人
        poStorereq.setCreator(poStorereq.getPkApppsnh());
        //制单日期
        poStorereq.setDmakedate(format);
        poStorereq.setCreationtime(format);
        //保存单据
        poStorereqService.insertPoStorereq(poStorereq);

        List<PoStorereqB> poStorereqBs = requestPurchase.getPoStorereqBs();
        int i = 0;
        for (PoStorereqB poStorereqB : poStorereqBs) {
            i++;
            //明细表设置主键ID
            String uUid = DocumentUtils.getUUid();
            poStorereqB.setCrowno(String.valueOf(i));
            poStorereqB.setPkStorereqB(uUid);
            poStorereqB.setPkApppsn(poStorereq.getPkApppsnh());
            //设置项目
            poStorereqB.setCprojectid(poStorereq.getPkProject());
            //设置制单日期
            poStorereqB.setDbilldate(poStorereq.getDbilldate());
            //需求日期
            poStorereqB.setDreqdate(poStorereq.getVdef15());
            //明细表设置表头主键ID
            poStorereqB.setPkStorereq(purchaseId);
            poStorereqB.setPkGroup(pkGrop);
            //获取物资主键
            String pkMaterial = poStorereqB.getPkMaterial();
            BdMaterial bdMaterial = bdMaterialService.getBdMaterialById(pkMaterial);
            //设置物资住单位
            poStorereqB.setCunitid(bdMaterial.getPkMeasdoc());
            poStorereqB.setCastunitid(bdMaterial.getPkMeasdoc());
            //设置物质数量
            poStorereqB.setNnum(poStorereqB.getNnum());
            poStorereqB.setNastnum(poStorereqB.getNnum());
            //设置库存版本号
            poStorereqB.setPkOrg(poStorereq.getPkOrg());
            poStorereqB.setPkOrgV(orgStockorgV);
            //原始需求库存组织
            poStorereqB.setPkReqstoorg(poStorereq.getPkOrg());
            poStorereqB.setPkReqstoorgV(orgStockorgV);
            poStorereqB.setDbilldate(format);
            poStorereqB.setDreqdate(format);
            //申请部门
            poStorereqB.setPkApppsn(poStorereq.getPkApppsnh());
            poStorereqB.setPkAppdept(poStorereq.getPkAppdepthV());
            poStorereqB.setPkAppdeptV(deptV);
            poStorereqBService.insertPoStorereqB(poStorereqB);
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult saveReceive(RequestReceive requestReceive) {
        String pkGrop = bdMaterialService.selectPkGrop();
        IcSapplyH icSapplyH = requestReceive.getIcSapplyH();
        //设置出库唯一主键
        String receiveId = DocumentUtils.getReceiveId();
        icSapplyH.setCgeneralhid(receiveId);//主键ID
        //设置库存保本号
        String orgStockorgV = selectBoxService.getOrgStockorgV(icSapplyH.getPkOrg());
        icSapplyH.setPkOrgV(orgStockorgV);
        //发料库存组织最新版本
        icSapplyH.setCissueorg(icSapplyH.getPkOrg());
        icSapplyH.setCissueorgV(orgStockorgV);
        //部门信息
        String deptV = selectBoxService.getDeptV(icSapplyH.getCdptid());
        icSapplyH.setCdptvid(deptV);
        //公司最新版本
        icSapplyH.setCorpoid(icSapplyH.getPkOrg());
        icSapplyH.setCorpvid(orgStockorgV);
        //创建时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        icSapplyH.setCreationtime(format);
        //创建人
        icSapplyH.setCreator(icSapplyH.getCbizid());
        //制单人
        icSapplyH.setBillmaker(icSapplyH.getCbizid());
        //设置状态
        icSapplyH.setFbillflag(2);
        //制单时间
        icSapplyH.setDmakedate(format);
        icSapplyH.setDbilldate(format);
        //设置出库单号
        String deliveryNumber = DocumentUtils.getDeliveryNumber();
        icSapplyH.setVbillcode(deliveryNumber);//出库单号
        //设置集团信息
        icSapplyH.setPkGroup(pkGrop);
        iIcSapplyHService.insertIcSapplyH(icSapplyH);
        List<IcSapplyB> icSapplyBs = requestReceive.getIcSapplyBs();
        int i = 0;
        for (IcSapplyB icSapplyB : icSapplyBs) {
            i++;
            //设置唯一主键
            String uUid = DocumentUtils.getUUid();
            icSapplyB.setCgeneralbid(uUid);
            //设置表头的唯一主键
            icSapplyB.setCgeneralhid(receiveId);
            //设置集团信息
            icSapplyB.setPkGroup(pkGrop);
            icSapplyB.setPkOrg(icSapplyH.getPkOrg());
            icSapplyB.setPkOrgV(orgStockorgV);
            icSapplyB.setCmaterialvid(icSapplyB.getCmaterialoid());
            icSapplyB.setCprojectid(icSapplyH.getCprojectid());
            icSapplyB.setCrowno(String.valueOf(i));
            //表头主键
            icSapplyB.setCsourcebillhid(receiveId);
            //单据类型
            icSapplyB.setCsourcetype(icSapplyH.getCtrantypeid());
            //设置单位
            String cmaterialoid = icSapplyB.getCmaterialoid();
            BdMaterial bdMaterial = bdMaterialService.getBdMaterialById(cmaterialoid);
            icSapplyB.setCastunitid(bdMaterial.getPkMeasdoc());
            //设置项目
            icSapplyB.setCprojectid(icSapplyH.getCprojectid());
            //设计金额
            iIcSapplyBService.insertIcSapplyB(icSapplyB);
        }
        return AjaxResult.success();
    }
}
